package com.study.springboottest.pdf;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.security.core.parameters.P;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class PdfToImage {

    public static void main(String[] args) {
        String pdfPath = "E:\\pdf\\离职证明.pdf";
        String outputFolder = "E:\\pdf";

        try {
            PDDocument document = PDDocument.load(new File(pdfPath));
            PDFRenderer renderer = new PDFRenderer(document);

            for (int page = 0; page < document.getNumberOfPages(); page++) {
                BufferedImage bufferedImage = renderer.renderImageWithDPI(page, 300);
                File outputFile = new File(outputFolder + "/page_" + (page + 1) + ".png");
                ImageIO.write(bufferedImage, "PNG", outputFile);
            }
            document.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
